﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.IO;
using System.Reflection;

using Refix.Core.Repository;
using Refix.RepositoryBrowser.Models;

namespace Refix.RepositoryBrowser.Presenters
{
    public class BrowserPresenter : IBrowserPresenter
    {
        public IRepositoryModel Model { get; set; }
        public IRepositoryModelFactory RepositoryModelFactory { get; set; }
        public ILocalRepository Repository { get; set; }

        public void SetDirectory(string directory)
        {
            RepositoryModelFactory.SetDirectory(directory);
            RepositoryItem repositoryItem = RepositoryModelFactory.ConstructModel(Model);
            Model.ReportStatus("Rendering view");
            Model.Repository = repositoryItem;
            Model.ReportStatus(null);
            Repository = new LocalRepository(directory);
        }

        public void SelectRepositoryItem(RepositoryItem item)
        {
            Model.SelectedItem = item;
        }

        public void RemoveRepositoryItem(RepositoryItem item)
        {
            // TODO: Implement this
            throw new NotImplementedException();
        }

        public bool UploadRepositoryItem(RepositoryItem item)
        {
            // TODO: Implement this
            throw new NotImplementedException();
        }

        public void SynchronizeRepositoryItem(RepositoryItem item)
        {
            // TODO: Implement this
            throw new NotImplementedException();
        }

        public void SaveRepositoryItem(RepositoryItem item)
        {
            Repository.PutComponent(item.Parent.Component);
            Model.SelectedItem = item;
        }

        public void AddFileToRepository(RepositoryItem item, string fileName)
        {
            File.Copy(fileName, Path.Combine(item.Directory, Path.GetFileName(fileName)));
            Model.SelectedItem = item;
        }

        public void RemoveFileFromRepository(RepositoryItem item, string fileName)
        {
            File.Delete(Path.Combine(item.Directory, fileName));
            Model.SelectedItem = item;
        }

        public bool RegisterRepositoryAssembly(string fileName)
        {
            Assembly assembly = null;
            try
            {
                assembly = Assembly.LoadFrom(fileName);
            }
// ReSharper disable EmptyGeneralCatchClause
            catch {}
// ReSharper restore EmptyGeneralCatchClause
            if (assembly == null)
            {
                Model.ReportError("Not a valid assembly:\n\n" + fileName);
                return false;
            }
            return Repository.RegisterAssembly(assembly, null);
        }
    }
}